package com.young.springdemo.service.statePattern;

//透支状态,存,取,计息
public class OverDraftAccountState extends AccountState {

    public OverDraftAccountState(AccountState state) {
        this.account = state.account;
    }

    public OverDraftAccountState(Account account) {
        this.account = account;
    }

    @Override//存钱
    public void deposit(double amount) {
        System.out.println(this.account.getOwner() + "存款" + amount);
        this.account.setBalance(account.getBalance() + amount);
        stateCheck();
    }

    @Override//取钱
    public void withdraw(double amount) {
        System.out.println(this.account.getOwner() + "取钱" + amount);
        this.account.setBalance(account.getBalance() - amount);
        stateCheck();
    }

    @Override//计算利息
    public void computerInterest() {
        System.out.println("透支状态,开始计算利息");
    }

    @Override//状态切换
    public void stateCheck() {
        if (this.account.getBalance() > 0) {
            this.account.setState(new NormalAccountState(this));
            System.out.println("状态切换为正常状态");
        } else if (this.account.getBalance() < -2000) {
            this.account.setState(new RestrictedStateAccountState(this));
            System.out.println("账户状态切换为受限状态");
        }
    }
}
